package com.csust.senior.dao.impl;

import com.csust.senior.dao.BaseDAO;
import com.csust.senior.dao.EmployeeDao;
import com.csust.senior.pojo.Employee;

import java.sql.SQLException;
import java.util.List;

public class EmployeeDaoImpl extends BaseDAO implements EmployeeDao {

    @Override
    public List<Employee> SelectAll() {
//        return List.of();
        try {
            String sql = "SELECT `emp_id` as empId,`emp_name` as empName,`emp_salary` as empSalary,`emp_age` as empAge FROM `t_emp`";
            return executeQuery(sql,Employee.class,null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public Employee getEmployeeById(int EmpId) {
        try {
            String sql = "SELECT `emp_id` as empId,`emp_name` as empName,`emp_salary` as empSalary,`emp_age` as empAge FROM `t_emp` where `emp_id` = ?";

            return executeQueryBean(sql,Employee.class,EmpId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int insertEmployee(Employee employee) {
        try {
            String sql = "INSERT INTO `t_emp` (`emp_name`,`emp_salary`,`emp_age`) VALUES (?,?,?)";
            return executeUpdate(sql,employee.getEmpName(),employee.getEmpSalary(),employee.getEmpAge());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateEmployee(Employee employee) {
        try {
            String sql = "UPDATE `t_emp` SET `emp_salary` = ? WHERE `emp_id` = ? ";
            return executeUpdate(sql,employee.getEmpSalary(),employee.getEmpId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteEmployee(int EmpId) {
        try {
            String sql = "DELETE FROM `t_emp` WHERE `emp_id` = ?";

            return executeUpdate(sql,EmpId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
